class Solution {
public:
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        if(root==nullptr) return false;
        return isSametree(root,subRoot)||
                isSubtree(root->left,subRoot)||
                isSubtree(root->right,subRoot);
    }

    bool isSametree(TreeNode* p, TreeNode* q)
    {
        if(p==nullptr||q==nullptr) return p==q;

         return p->val==q->val&&
         isSametree(p->left,q->left)&&
         isSametree(p->right,q->right);
    }
    
};